<?php

namespace Main\Api\App;

use FC\Json;

// 控制器基类
use FC\Controller\BaseController;

class common extends BaseController
{
	public $config, $template_name, $template_dir, $page_size;

	function __construct()
	{
		// 关闭跨域
		//$this->cross = false;

		// 实例化父类构造
		parent::__construct();

		// 读取配置
		$this->config = $this->getConfig();
		
		// 这里用于邮件发送
		$this->config['smtp_secure'] = 'ssl';
		$this->config['smtp_auth'] = true;		

		// 文件存储位置
		$this->upload_path = PATH['NOW'] . '/Uploads/';

		// 文件访问地址
		$this->upload_url = HOST_DOMAIN . HOST_DIR . '/Uploads/';

		// 文章缩略图存储位置
		$this->cover_path = PATH['NOW'] . '/Cover/';

		// 文章缩略图访问地址
		$this->cover_url = HOST_DOMAIN . HOST_DIR . '/Cover/';

		$this->template_dir = PATH['NOW'] . '/Themes/';

		if ($this->config['status'] == 1) {
			$this->VIEW->setDirName('default');
			$this->VIEW->display('maintain');
			die();
		}
		
		// 配置文件
        $config_ini_file = PATH['NOW'] . '/Themes/thems_config.ini';
        
        if (is_file($config_ini_file)) {
            $ini_config = parse_ini_file($config_ini_file);
			$this->page_size = isset($ini_config['config']['page_size']) ? $ini_config['config']['page_size'] : 10;
        }
		
		// 模板名称
		$this->template_name = 'default';
		
		if(!empty($ini_config['template_name'])){
            $this->template_name = $ini_config['template_name'];
	    }
		
		$this->template_dir .= $this->template_name; // 模板目录

		//加载初始化类
		$load = \FC\obj('FC\Glue\Load');

		//第三方扩展设置
		$load->ExtendConfig($this->template_dir . '/php/loader.php');
	}

	// 获取配置
	public function getConfig()
	{
		// 文件缓存类
		$fileCache = $this->FCACHE;
		// 缓存名称
		$cache_name = 'fc_config';
		if ($fileCache->has($cache_name)) {
			return $fileCache->get($cache_name);
		}
		$res = $this->config_model->getTypeId();
		$config = [];
		foreach ($res as $k => $v) {
			$config[$v['cfg']] = $v['content'];
		}
		$fileCache->set($cache_name, $config);
		return $config;
	}

	// 模板文件
	public function views($template)
	{
		$this->VIEW->setDirName($this->template_name);
		$this->VIEW->assign('config', $this->config);
		$this->VIEW->assign('COVER_URL', $this->cover_url);
		$static_url = HOST_DIR . '/Themes/' . $this->template_name . '/static';
		$this->VIEW->assign('THEME_URL', $static_url);
		$this->VIEW->display($template);
	}

	// 设置模板变量
	public function assign($key, $value)
	{
		if (empty($key) || empty($value)) {
			return false;
		}
		$this->VIEW->assign($key, $value);
	}

	// rss
	public function rssIndex()
	{
		$this->VIEW->setDirName('default');
		$this->VIEW->assign('config', $this->config);
		\FC\head('xml');
		echo '<?xml version="1.0" encoding="utf-8"?>';
		$this->VIEW->display('rss');
	}
	
    /**
     * 错误提示
     *
     * @param [type] $msg
     * @return void
     */
    public function error($code, $msg)
    {
        Json::error($code, $msg);
    }

    /**
     * 成功提示
     *
     * @param [type] $msg
     * @return void
     */
    public function success($data, $msg = '', $append = [], $code = 0)
    {
        Json::result($data, $code, $msg, $append);
    }

    /**
     * 创建文件夹
     * @return string
     */
    protected function creDir($path)
    {
        $dir = dirname($path);
        if (!is_dir($dir)) {
            if (!mkdir($dir, 0777, true)) {
                return false;
            }
        }
        return true;
    }		
}
